#!/bin/bash

echo -e "${GREEN}\${g_args[@]} = ${g_args[*]}${NORMAL}" #print variable
echo -e "${GREEN}\${e_args[@]} = ${e_args[*]}${NORMAL}" #print variable
echo -e "${GREEN}\${f_args[@]} = ${f_args[*]}${NORMAL}" #print variable
echo -e "${GREEN}\${ARGS[@]} = ${ARGS[*]}${NORMAL}"     #print variable
echo -e "${GREEN}\${dir_fns_} = ${dir_fns_}${NORMAL}"   #print variable

echo "that ${FNN}.sal"

amount_arg ${#ARGS[@]} 4 5

count=0

IFS='
'
# prn_list_2_ "${ARGS[0]}" "${ARGS[1]}"

for item in $(prn_list_2_ "${ARGS[1]}" "${ARGS[2]}"); do

    # echo -e "${GREEN}\$item = $item${NORMAL}" #print variable

    if [ 0 -eq "$count" ]; then
        src=$HOME/$item
        count=1
        continue
    fi
    if [ 1 -eq "$count" ]; then

        if [ -n "${ARGS[4]}" ]; then

            mkdir_ "$HOME/$item/${ARGS[4]}/"

            dest="$HOME/$item/${ARGS[4]}"
        else
            mkdir_ "$HOME/$item/own_plt/"

            dest="$HOME/$item/own_plt"
        fi

        # echo "$arg_1 $arg_2"

        dir_src="$(dirname "$src")"
        file_src="$(basename "$src")"
        dir_dest="$(dirname "$dest")"
        file_dest="$(basename "$dest")"

        # echo -e "${GREEN}\$dir_src = $dir_src${NORMAL}"     #print variable
        # echo -e "${GREEN}\$file_src = $file_src${NORMAL}"   #print variable
        # echo -e "${GREEN}\$dir_dest = $dir_dest${NORMAL}"   #print variable
        # echo -e "${GREEN}\$file_dest = $file_dest${NORMAL}" #print variable

        # echo -e "${BLUE}--- cp $arg_1 $arg_2 ---${NORMAL}" #sistem info mesage
        if [ "${ARGS[0]}" == "-b" ]; then

            if [ -d "$src" ] && [ -d "$dest/$file_src" ]; then
                echo -e "${BLUE}--- rm -rf $dest/$file_src ---${NORMAL}" #sistem info mesage

                rm -rf "$dest/${file_src:?}"
            fi

            echo -e "${HLIGHT}--- cp -rf $src $dest ---${NORMAL}" #start filesgit
            cp -rf "$src" "$dest"
        elif [ "${ARGS[0]}" == "-d" ]; then

            #start files
            if [ -e "$dest/$file_src" ]; then
                if [ -e "$src" ]; then
                    sth_fd_ "$src" "${ARGS[3]}"
                    rm -rf "${src:?}"
                fi
                # echo -e "${BLUE}--- cp -rf /home/st/$dest/$file_src /home/st/$dir_src ---${NORMAL}" #sistem info mesage
                echo -e "${BLUE}--- git checkout HEAD $dest/$file_src ---${NORMAL}" #sistem info mesage
                git checkout HEAD "$dest/$file_src"
                echo -e "${BLUE}--- cp -rf $dest/$file_src" "$dir_src ---${NORMAL}" #sistem info mesage
                cp -rf "$dest/$file_src" "$dir_src"
            else
                qq_exit "EXIT bckp_list_: $dest/$file_src not exist -> do bckp_list_() -b exactly"
            fi

            # echo -e "${HLIGHT}--- cp -ru --backup=t /home/st/$dest/$file_src /home/st/$dir_src ---${NORMAL}"
            # sth_fd_ "/home/st/$src" 3
            # cp -ru "/home/st/$dest/$file_src" "/home/st/$dir_src"
        else
            qq_exit "${ARGS[0]} is not (-b)ckup or (-d)eploy"
        fi
        count=0
        continue
    fi
    # count=$((count +1))
done
